package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.jnzc.ocr.common.bean.Tree;
import com.jnzc.ocr.common.utils.ArrayHandleUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.dependencies.serv.mybatis.MybatisServiceImpl;
import com.jnzc.ocr.serv.shop.entity.*;
import com.jnzc.ocr.serv.shop.filter.ShopProductFilter;
import com.jnzc.ocr.serv.shop.mapper.ShopProductMapper;
import com.jnzc.ocr.serv.shop.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jnzc.ocr.serv.shop.vo.ShopProductInfoVo;
import com.jnzc.ocr.serv.shop.vo.ShopProductSaveVo;
import com.jnzc.ocr.serv.shop.vo.ShopProductStockChangeVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author zhangcm
 * @version 1.0, 2017-09-29
 */
@Service
public class ShopProductServiceImpl extends MybatisServiceImpl<ShopProductMapper, ShopProduct, ShopProductFilter> implements ShopProductService {

    @Autowired
    private ShopProductSkuService shopProductSkuService;

    @Autowired
    private ShopSupplierService shopSupplierService;

    @Autowired
    private ShopProductCategoryService shopProductCategoryService;

    @Autowired
    private ShopProductAttrService shopProductAttrService;

    @Autowired
    private ShopProductStockService shopProductStockService;

    @Override
    protected void fillEntityWrapper(EntityWrapper<ShopProduct> ew, ShopProductFilter filter) {
        ShopProduct entity = new ShopProduct();
        entity.setId(filter.getId());
        entity.setShopId(filter.getShopId());
        entity.setSupplierId(filter.getSupplierId());
        entity.setCategoryId(filter.getCategoryId());
        entity.setIfOnSale(filter.getIfOnSale());
        ew.setEntity(entity);
        if(CollectionUtils.isNotEmpty(filter.getIdList())) {
            ew.in("id", filter.getIdList());
        }
    }

    @Override
    public ShopProduct find(Long id, Long shopId) {
        ValidateUtils.isId(id);
        ShopProductFilter filter = new ShopProductFilter();
        filter.setId(id);
        filter.setShopId(shopId);
        return super.findNotNull(filter);
    }

    @Override
    public ShopProductInfoVo findInfo(Long id, Long shopId) {
        ShopProductInfoVo result = new ShopProductInfoVo();
        ShopProduct obj = find(id, shopId);
        result.setProduct(obj);
        List<Tree<ShopProductAttr>> attrTree = shopProductAttrService.listTree(obj.getId(), obj.getCategoryId(), shopId);
        result.setAttrList(attrTree);
        List<ShopProductSku> skuList = shopProductSkuService.listWithAttr(id, shopId, attrTree);
        result.setSkuList(skuList);
        return result;
    }

    @Override
    @Transactional
    public ShopProduct save(ShopProductSaveVo param, Long shopId) {
        // 检查
        ValidateUtils.notEmpty(param.getNo(), "编号");
        ValidateUtils.notEmpty(param.getName(), "名称");
        ValidateUtils.notEmpty(param.getImageUrl(), "主图");
        ValidateUtils.notNull(param.getPrice(), "价格");
        ValidateUtils.isId(param.getCategoryId(), "分类ID");
        ValidateUtils.notEmpty(param.getSkuList(), "SKU列表");
        ValidateUtils.isId(shopId, "店铺ID");
        // 保存
        ShopProduct product;
        if(null == param.getId()){
            product = new ShopProduct();
            product.setShopId(shopId);
            product.setCreateTime(DateUtils.getNow());
        } else {
            product = find(param.getId(), shopId);
        }
        if(null != param.getSupplierId()) {
            ShopSupplier supplier = shopSupplierService.find(param.getSupplierId(), shopId);
            product.setSupplierId(supplier.getId());
        }
        ShopProductCategory category = shopProductCategoryService.find(param.getCategoryId(), shopId);
        product.setCategoryId(category.getId());
        product.setNo(param.getNo());
        product.setName(param.getName());
        product.setImageUrl(param.getImageUrl());
        product.setPrice(param.getPrice());
        product.setPurchasePrice(param.getPurchasePrice());
        product.setUpdateTime(DateUtils.getNow());
        product.setIfLowStock(false);
        product.setIfOnSale(null == param.getIfOnSale() || param.getIfOnSale());
        this.save(product);
        // 保存属性
        shopProductAttrService.save(param.getSkuList(), product);
        // 保存SKU
        shopProductSkuService.save(param.getSkuList(), product);
        // 修改库存
        List<ShopProductStockChangeVo> changeList = Lists.newArrayList();
        for (ShopProductSaveVo.Sku skuParam : param.getSkuList()) {
            ValidateUtils.notNull(skuParam.getNum(), "SKU数量");
            ShopProductStockChangeVo change = new ShopProductStockChangeVo();
            change.setProductId(product.getId());
            change.setSkuId(skuParam.getId());
            change.setNewStockNum(skuParam.getNum());
            changeList.add(change);
        }
        shopProductStockService.updateNum(changeList, shopId);
        return product;
    }

    @Override
    public void delete(Long id, Long shopId) {
        ValidateUtils.isId(id);
        ShopProductFilter filter = new ShopProductFilter();
        filter.setId(id);
        filter.setShopId(shopId);
        super.delete(filter);
        // 删除SKU
        shopProductSkuService.delete(id, shopId);
    }

    @Override
    public List<ShopProduct> list(List<Long> idList, Long shopId) {
        if(CollectionUtils.isEmpty(idList)) {
            return Lists.newArrayList();
        }
        ShopProductFilter filter = new ShopProductFilter();
        filter.setIdList(idList);
        filter.setShopId(shopId);
        return super.list(filter);
    }

    @Override
    public Map<Long, ShopProduct> listMap(List<Long> idList, Long shopId) {
        List<ShopProduct> list = this.list(idList, shopId);
        return ArrayHandleUtils.parseMap(list, "id");
    }

    @Override
    public void clearLowStock(Long shopId) {
        ShopProduct updateObj = new ShopProduct();
        updateObj.setIfLowStock(false);
        ShopProduct whereObj = new ShopProduct();
        whereObj.setShopId(shopId);
        EntityWrapper<ShopProduct> ew = new EntityWrapper<ShopProduct>(whereObj);
        super.update(updateObj, ew);
    }

    @Override
    public void updateLowStock(List<Long> idList, Long shopId) {
        List<ShopProduct> list = this.list(idList, shopId);
        for (ShopProduct obj : list) {
            obj.setIfLowStock(true);
        }
        super.updateList(list);
    }

    @Override
    public void updateSale(Long id, Long shopId, Boolean ifOnSale) {
        ShopProduct product = find(id, shopId);
        product.setIfOnSale(ifOnSale);
        super.updateOne(product);
    }
}
